package cn.qayy.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.qayy.dao.BasisMapper;
import cn.qayy.dao.BidMapper;
import cn.qayy.dao.CargoInvoiceApplyMapper;
import cn.qayy.dao.CashFlowPMapper;
import cn.qayy.dao.CostPMapper;
import cn.qayy.dao.FocusOnMapper;
import cn.qayy.dao.GoodFriendMapper;
import cn.qayy.dao.PlanMapper;
import cn.qayy.dao.PolicyMapper;
import cn.qayy.dao.RegisterMapper;
import cn.qayy.dao.StandardMapper;
import cn.qayy.dao.TaskMapper;
import cn.qayy.dao.WxClientMapper;
import cn.qayy.dao.WxMailListMapper;
import cn.qayy.dao.WxPrizeMapper;
import cn.qayy.dto.CargoInvoiceApplyDTO;
import cn.qayy.dto.CashFlowDTO;
import cn.qayy.dto.ClientDTO;
import cn.qayy.dto.CostDTO;
import cn.qayy.dto.MailListDTO;
import cn.qayy.dto.PlanLogDTO;
import cn.qayy.dto.PolicyLogDTO;
import cn.qayy.dto.PrizeDTO;
import cn.qayy.dto.TaskDTO;
import cn.qayy.service.IPlanLogService;
import cn.qayy.service.IPolicyLogService;
@SuppressWarnings("all")
public class DownLoadUtil {
	/***
	 * 将查询参数绑定到session中
	 * @param list
	 */
	public static void set(String key,Object obj) {
		 getSession().setAttribute(key, obj);
		
	}
	/**
	 * 从session中获取最后一次查询的参数
	 * @return
	 */
	public static Object get(String key){
		return  getSession().getAttribute(key);
	}
	
	/***
	 * 单例设计模式
	 */
	private DownLoadUtil () {}
	private static DownLoadUtil instance = new DownLoadUtil();
	public static DownLoadUtil getInstance() {
		return instance;
	}
	/***
	 * 获取目标下载的数据list
	 * @param clazz 目标下载的模板 字节码对象
	 * @return
	 * @throws Exception
	 */
	public <T> List<T> getTargetDownLoadDataList(Class clazz) throws Exception{
		Map<String, Object> params = null;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<Object> downTarget = null;
		if(clazz.getSimpleName().equals("BasisDTO")) {//获取基础库数据
			BasisMapper mapper = (BasisMapper) SpringContextUtils.getBeanById("basisMapper");
			params = (Map<String, Object>)DownLoadUtil.get("BasisDTO");
			if(params == null) {
				params = new HashMap<String, Object>();
				params.put("t", "1127");
			}
			list = mapper.selectBasisBy(params);
		} else if(clazz.getSimpleName().equals("BidDTO")) {//获取中标库数据
			BidMapper mapper = (BidMapper) SpringContextUtils.getBeanById("bidMapper");
			params = (Map<String, Object>)DownLoadUtil.get("BidDTO");
			if(params == null) {
				params = new HashMap<String, Object>();
				params.put("t", "1127");
			}
			list = mapper.selectBidBy(params);
		} else if(clazz.getSimpleName().equals("PlansDTO")
				||"ReportPlans".equals(clazz.getSimpleName())) {//获取流向数据
			PlanMapper mapper = (PlanMapper) SpringContextUtils.getBeanById("planMapper");
			params = (Map<String, Object>)DownLoadUtil.get("PlansDTO");
			if(params == null) {
				params = new HashMap<String, Object>();
				params.put("t", "1127");
			}
			list = mapper.selectPlanBy(params);
		} else if(clazz.getSimpleName().equals("PolicyDTO")
				||"PolicyAllDTO".equals(clazz.getSimpleName())
				||"PolicyDTO_RE".equals(clazz.getSimpleName())) {//获取协议数据
			PolicyMapper mapper = (PolicyMapper) SpringContextUtils.getBeanById("policyMapper");
			params = (Map<String, Object>)DownLoadUtil.get("PolicyDTO");
			if(params == null) {
				params = new HashMap<String, Object>();
				params.put("t", "1127");
			}
			if("PolicyDTO_RE".equals(clazz.getSimpleName())) {
				list = mapper.selectRePolicyBy(params);
			} else {
				list = mapper.selectPolicyBy(params);
			}
		} else if(clazz.getSimpleName().equals("RegisterDTO")) {//获取注册库数据
			RegisterMapper mapper = (RegisterMapper) SpringContextUtils.getBeanById("registerMapper");
			params = (Map<String, Object>)DownLoadUtil.get("RegisterDTO");
			if(params == null) {
				params = new HashMap<String, Object>();
				params.put("t", "1127");
			}
			list = mapper.selectRegisterBy(params);
		} else if(clazz.getSimpleName().equals("StandardDTO")) {//获取标测库数据
			StandardMapper mapper = (StandardMapper) SpringContextUtils.getBeanById("standardMapper");
			params = (Map<String, Object>)DownLoadUtil.get("StandardDTO");
			if(params == null) {
				params = new HashMap<String, Object>();
				params.put("t", "1127");
			}
			list = mapper.selectStandardByDown(params);
		} else if(clazz.getSimpleName().equals("GFDTO")) {//获取好伙伴数据
			GoodFriendMapper mapper = (GoodFriendMapper) SpringContextUtils.getBeanById("goodFriendMapper");
			params = (Map<String, Object>) DownLoadUtil.get("GFDTO");
			if(params == null) {
				params = new HashMap<String, Object>();
				params.put("t", "520");
			}
			list = mapper.selectGFBy(params);
		} else if(clazz.getSimpleName().equals("FocusOnDTO")) {//获取重点终端数据
			FocusOnMapper mapper = (FocusOnMapper) SpringContextUtils.getBeanById("focusOnMapper");
			params = (Map<String, Object>)DownLoadUtil.get("FocusOnDTO");
			if(params == null) {
				params = new HashMap<String, Object>();
				params.put("t", "520");
			}
			list = mapper.selectFocusBy(params);
		} else if(clazz.getSimpleName().equals("ErrataDTO")) {
			PlanMapper mapper = (PlanMapper) SpringContextUtils.getBeanById("planMapper");
			params = new HashMap<String, Object>();
			params.put("t", "1127");
			list = mapper.selectFocusByMap(params);
		} else if(clazz.getSimpleName().equals("ClientBankDTO")) {//获取协议银行卡数据
			PolicyMapper mapper = (PolicyMapper) SpringContextUtils.getBeanById("policyMapper");
			params = new HashMap<String, Object>();
			params.put("t", "520");
			list = mapper.selectClientBankBy(params);
		} else if(clazz.getSimpleName().equals("PolicyDemoWCL")) {//下载协议备案复核完成率
			PolicyMapper mapper = (PolicyMapper) SpringContextUtils.getBeanById("policyMapper");
			params = new HashMap<String, Object>();
			params.put("t", "520");
			list = mapper.selectPolicyDemoWCLByMap(params);
		} else if(clazz.getSimpleName().equals("CashFlowDTO")) {//获取现金流数据
			CashFlowPMapper mapper = (CashFlowPMapper) SpringContextUtils.getBeanById("cashFlowPMapper");
			CashFlowDTO dto =(CashFlowDTO) DownLoadUtil.get("CashFlowDTO");
			if(dto != null) {	
				list = mapper.selectCashFlowByDown(dto);
			}
		}else if(clazz.getSimpleName().equals("ClientDTO")) {//获取好伙伴
			WxClientMapper mapper = (WxClientMapper) SpringContextUtils.getBeanById("wxClientMapper");
			Map<String, Object> mapDto =(Map<String, Object>) DownLoadUtil.get("ClientDTO");
			ClientDTO dto = (ClientDTO)mapDto.get("dto");
			if(dto != null) {	
				list = mapper.selectClientByDown(dto);
			}
		}else if(clazz.getSimpleName().equals("MailListDTO")) {//获取通讯录
			WxMailListMapper mapper = (WxMailListMapper) SpringContextUtils.getBeanById("wxMailListMapper");
			MailListDTO dto =(MailListDTO) DownLoadUtil.get("MailListDTO");
			if(dto != null) {	
				list = mapper.selectMailListByDown(dto);
			}
		}else if(clazz.getSimpleName().equals("PrizeDTO")) {//获取微信抽奖奖品
			WxPrizeMapper mapper = (WxPrizeMapper) SpringContextUtils.getBeanById("wxPrizeMapper");
			PrizeDTO dto =(PrizeDTO) DownLoadUtil.get("PrizeDTO");
			if(dto != null) {	
				list = mapper.selectPrizeByDown(dto);
			}
		}else if(clazz.getSimpleName().equals("CargoInvoiceApplyDownMB")) {//货票申请下载模板
			CargoInvoiceApplyMapper mapper = (CargoInvoiceApplyMapper) SpringContextUtils.getBeanById("cargoInvoiceApplyMapper");
			CargoInvoiceApplyDTO dto =(CargoInvoiceApplyDTO) DownLoadUtil.get("CargoInvoiceApplyDTO");
			if(dto != null) {	
				list = mapper.selectForDown(dto);
			}
		}else if(clazz.getSimpleName().equals("CostDTO")) {
			CostPMapper mapper = (CostPMapper) SpringContextUtils.getBeanById("costPMapper");
			CostDTO dto = (CostDTO) DownLoadUtil.get("CostDTO");
			if(dto != null) {	//如果筛选条件为空，则下载空白
				list = mapper.selectForDown(dto);
			}
		}else if(clazz.getSimpleName().equals("Report3yearPlans")) {
			PlanMapper mapper = (PlanMapper) SpringContextUtils.getBeanById("planMapper");
			params =new HashMap<String, Object>();
			List<Integer> years = new ArrayList<Integer>();
			int year = DateUtils.getYear(new Date());
			//years.add(year-1);
			//years.add(year-2);
			years.add(year);
			params.put("year",years);
			list = mapper.selectPlanBy(params);
		} else if("TaskDTO".equals(clazz.getSimpleName())) {
			TaskMapper mapper = (TaskMapper) SpringContextUtils.getBeanById("taskMapper");
			TaskDTO dto =(TaskDTO) DownLoadUtil.get("TaskDTO");
			if(dto != null) {	
				list = mapper.selectListByDown(dto);
			}
		} else if("PolicyLogDTO".equals(clazz.getSimpleName())) {
			IPolicyLogService logService = (IPolicyLogService) SpringContextUtils.getBeanById("policyLogServiceImpl");
			PolicyLogDTO dto =(PolicyLogDTO) DownLoadUtil.get("PolicyLogDTO");
			if(dto != null) {	
				list = logService.selectListByDown(dto);
			}
		}else if("PlanLogDTO".equals(clazz.getSimpleName())) {
			IPlanLogService logService = (IPlanLogService) SpringContextUtils.getBeanById("planLogServiceImpl");
			PlanLogDTO dto =(PlanLogDTO) DownLoadUtil.get("PlanLogDTO");
			if(dto != null) {
				list = logService.selectListByDown(dto);
			}
		}
		
		
		if(list != null) {
			/**
			 * 如果list.size()==0,则下载空数据的模板
			 */
			downTarget = new ArrayList<Object>(list.size()+1);
			for (Map<String, Object> map : list) {
				Object newInstance = clazz.newInstance();
				BeanUtils.populate(newInstance, map);
				downTarget.add(newInstance);
			}
		}
		return (List<T>) downTarget;
	}
	/***
	 * 获取session
	 * @return
	 */
	public static HttpSession getSession(){
        //获取到ServletRequestAttributes 里面有 
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //获取到Request对象
        HttpServletRequest request = attrs.getRequest();
        //获取到Session对象
        HttpSession session = request.getSession();

        //获取到Response对象
        //HttpServletResponse response = attrs.getResponse();
        return session;
    }
}